package yanc.app

import grails.converters.JSON
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class MenuController {
    def menuService
    static final Logger logger = LoggerFactory.getLogger(MenuController.class)

    def getMenu() {
        def data = [:]
        def account = "${params.account ?: ''}"
        session.user = account
        try {
            def user = User.findByAccount(account)
            def roles = []
            def m = []
            if (user.rolesId) {
                roles = user.rolesId.split(",")
                roles.each { roleId ->
                    println roleId
                    def role = Roles.findByRoleId(roleId)
                    def pers = role.perId.split(",")
                    pers.each { pid ->
                        def maps = menuService.ser(pid)
                        m.add(maps)
                    }
                }
            }
            data << [data: m]
            render data as JSON
        } catch (Exception e) {
            logger.error("MenuController -- getMenu Exception " + e.message)
        }
    }

    def getAll() {
        def data = [:]
        def list = []
        try {
            def lists = Menu.findAllByTypes("A")
            lists.each { menu ->
                def maps = menuService.ss(menu)
//                maps.put("label", menu.name)
                maps.put("value", menu.permissionId)
                maps.put("description", menu.description)
                list.add(maps)
            }
            data << [statuCode: "0", errMsg: "0", data: list]
            render data as JSON
        } catch (Exception e) {
            logger.error("MenuController -- getAll Exception " + e.message)
        }

    }
}
